package patrol.mobile.com.mobilepatrol.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import patrol.mobile.com.mobilepatrol.R;

/**
 * Created by apple on 17/11/6.
 */

public class PopupUtil {

    static PopupWindow weatherPopup;
    /**
     * 初始化popup窗口
     */
    public static void initSelectPopup(final Context ctx, final List<String> weatherData, TextView tv_weather, final PopListener listener) {

        ListView mLv = new ListView(ctx);
        // 设置适配器
        ArrayAdapter selAdapter = new ArrayAdapter<String>(ctx, R.layout.popup_text_item, weatherData);
        mLv.setAdapter(selAdapter);

        // 设置ListView点击事件监听

        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 在这里获取item数据
                String value = weatherData.get(position);
                // 把选择的数据展示对应的TextView上
                 listener.getData(value,weatherPopup);
                // 选择完后关闭popup窗口
                weatherPopup.dismiss();
            }
        });
        weatherPopup = new PopupWindow(mLv, tv_weather.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 取得popup窗口的背景图片
        Drawable drawable = ContextCompat.getDrawable(ctx, R.drawable.bg1);
        weatherPopup.setBackgroundDrawable(drawable);
        weatherPopup.setFocusable(true);
        weatherPopup.setOutsideTouchable(true);
        weatherPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 关闭popup窗口
                weatherPopup.dismiss();
            }
        });
    }
    public interface PopListener{
        void getData(String s,PopupWindow weatherPopup);
    }
}
